/// Setting up the ANES File for presidential coalitions  

use "/Users/joshuazingher/Desktop/Prospectus /anes_cdfdta/anes coalitions .dta"

sort year

rename VCF0704 presvote

rename VCF0703 voted


/// Generating total Black respondants by year

by year: egen blackresp = count(race) if race == 2

/// Generating total number of Black voters by year


by year: egen Blackvoted = count(voted) if race==2 & voted==3

/// Generating total number of Latnio non-voters by year

by year: egen BlackNvoted = count(voted) if voted==0 & race==2 | voted==1 & race==2 | voted==2 & race==2

/// Generating total number of non-Black voters by year

by year: egen NBlackvoted = count(voted) if race~=2 & voted==3

/// Generating total number of non-Black non-voters by year

by year: egen NBlackNvoted = count(voted) if race~=2 & voted==0 | race~=2 & voted==1 | race~=2 & voted==2

/// filling in the blanks

by year: egen blackvoted = max(Blackvoted)
by year: egen blackNvoted = max(BlackNvoted)
by year: egen Nblackvoted = max(NBlackvoted)
by year: egen NblackNvoted = max(NBlackNvoted)




/// Generating % Black TO with Mosteller Correction

gen N = 1
gen a = [blackvoted * NblackNvoted] / [blackNvoted * Nblackvoted]


///Generating filler vars

gen aminone =(a-1)
gen ABlackplusTO = (ABlack+aTO)
gen aBlackTO=(a*ABlack*aTO)

gen CBlackTO = 1/ (2*(aminone)) * (N+(aminone*ABlackplusTO) - sqrt((N+aminone*ABlackplusTO)*(N+aminone*ABlackplusTO) - (4*(aminone*aBlackTO))))

///Adjusting TO for the formula

replace CBlackTO = CBlackTO/ABlack



/// Moving to Partisanship - Generating Black Democratic Vote total by year

by year: egen Blackdem = count(race) if presvote==1 & race==2

/// Generating Black Non-Democratic Vote by Year

by year: egen BlackNdem = count(race) if presvote==2 & race==2

// Generating non-Black Democratic Vote total by year

by year: egen NBlackdem = count(race) if presvote==1 & race~=2

/// Generating non-Black non-Democratic vote total by year

by year: egen NBlackNdem = count(race) if presvote==2 & race~=2

/// filling in the blanks

by year: egen blackDem = max(Blackdem)
by year: egen blackNDem = max(BlackNdem)
by year: egen NblackDem = max(NBlackdem)
by year: egen NblackNDem = max(NBlackNdem)

/// Generation % Black Democrat with Mosteller Correction

gen A = (blackDem*NblackNDem)/(blackNDem*NblackDem)
gen Aminone =(A-1)
gen ABlackplusDem = (ABlack+ADem)
gen ABlackDem=(A*ABlack*ADem)
gen CBlackDem = 1/(2*(Aminone)) * (N+(Aminone*ABlackplusDem) - sqrt((N+Aminone*ABlackplusDem)*(N+Aminone*ABlackplusDem) - (4*(Aminone*ABlackDem))))

replace CBlackDem = CBlackDem/ABlack


/// Generating number of ANES respondents by year

by year: egen totalresp = count (VCF0006a)


/// Coalition Contribution

gen blackcoalcontrib = (CBlackTO* CBlackDem *ABlack)/(aTO*ADem)



